package com.mycompany.webapp.action;

import java.util.HashMap;

import org.junit.Before;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.web.context.WebApplicationContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionProxy;

@ContextConfiguration(locations = {
		"classpath:/applicationContext-resources.xml",
        "classpath:/applicationContext-dao.xml",
        "classpath:/applicationContext-service.xml"
})
public abstract class AppTransactionalStrutsTestCase extends StrutsSpringTransactionalTests {

  @Before
  public void onSetUp() throws Exception {
    super.setUp();
    setupAction();
  }

  protected abstract void setupAction() throws DataAccessException;

  @Override
  protected void setupBeforeInitDispatcher() throws Exception {
    servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, applicationContext);
  }

  protected ActionProxy initActionProxy(String uri) {
    ActionProxy proxy = getActionProxy(uri);
    ActionContext actionContext = proxy.getInvocation().getInvocationContext();
    actionContext.setSession(new HashMap<String, Object>());
    return proxy;
  }

  @Override
  //@Autowired
  //public void setDataSource(@Qualifier(value = "appDataSource") DriverManagerDataSource appDataSource) {
  public void setDataSource(DriverManagerDataSource dataSource) {
    this.dataSource = dataSource;
    this.simpleJdbcTemplate = new SimpleJdbcTemplate(dataSource);
  }
}
